﻿using BXH.Movie.Bll;
using BXH.Movie.FrmAdmin;
using BXH.Movie.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;

namespace BXH.Movie.FrmAdmin
{
    public partial class FrmMainAdmin : Form
    {
        public FrmMainAdmin()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
        }

        private MovieInfoService movieService = new MovieInfoService();
        private MovieSessionService movieSessionService = new MovieSessionService();
        private UserInfoService userInfoService = new UserInfoService();
        private void FrmMainAdmin_Load(object sender, EventArgs e)
        {
            //dgvMovie
            var movies = movieService.GetMovieInfos();
            dgvMovie.DataSource = movies;

            dgvMovie.AutoGenerateColumns = false;
            DataGridViewLinkColumn dgvMovieLink = new DataGridViewLinkColumn();
            dgvMovieLink.HeaderText = "删除";//列的标题
            dgvMovieLink.Text = "删除";
            dgvMovieLink.UseColumnTextForLinkValue = true;//上面设置的dlink.Text文字在列中显示
            dgvMovie.Columns.Add(dgvMovieLink);//将创建的列添加到UserdataGridView中

            //dgvType

            var movieTypes = movieService.GetAllMovieType();
            dgvType.DataSource = movieTypes;

            dgvType.AutoGenerateColumns = false;
            DataGridViewLinkColumn dgvTypeLink = new DataGridViewLinkColumn();
            dgvTypeLink.HeaderText = "删除";//列的标题
            dgvTypeLink.Text = "删除";
            dgvTypeLink.UseColumnTextForLinkValue = true;//上面设置的dlink.Text文字在列中显示
            dgvType.Columns.Add(dgvTypeLink);//将创建的列添加到UserdataGridView中


            //dgvSession
            var sessions = movieSessionService.GetAllSession();
            dgvSession.DataSource = sessions;

            dgvSession.AutoGenerateColumns = false;
            DataGridViewLinkColumn dgvSessionLink = new DataGridViewLinkColumn();
            dgvSessionLink.HeaderText = "删除";//列的标题
            dgvSessionLink.Text = "删除";
            dgvSessionLink.UseColumnTextForLinkValue = true;//上面设置的dlink.Text文字在列中显示
            dgvSession.Columns.Add(dgvSessionLink);//将创建的列添加到UserdataGridView中


            //dgvAdmin
            var admins = userInfoService.GetAdmins();
            dgvAdmin.DataSource = admins;

            dgvAdmin.AutoGenerateColumns = false;
            DataGridViewLinkColumn dgvAdminLink = new DataGridViewLinkColumn();
            dgvAdminLink.HeaderText = "删除";//列的标题
            dgvAdminLink.Text = "删除";
            dgvAdminLink.UseColumnTextForLinkValue = true;//上面设置的dlink.Text文字在列中显示
            dgvAdmin.Columns.Add(dgvAdminLink);//将创建的列添加到UserdataGridView中


            dgvMovie.CellMouseClick += DgvMovie_CellMouseClick;
            dgvType.CellMouseClick += DgvType_CellMouseClick;
            dgvSession.CellMouseClick += DgvSession_CellMouseClick;
            dgvAdmin.CellMouseClick += DgvAdmin_CellMouseClick;

            dgvMovie.CellValueChanged += DgvMovie_CellValueChanged;
            dgvType.CellValueChanged += DgvType_CellValueChanged;
            dgvSession.CellValueChanged += DgvSession_CellValueChanged;
            dgvAdmin.CellValueChanged += DgvAdmin_CellValueChanged; ;

            //dgvType.Visible = false;
            //dgvSession.Visible = false;
            //dgvAdmin.Visible = false;


            dgvMovie.Columns[0].ReadOnly =
            dgvType.Columns[0].ReadOnly =
            dgvSession.Columns[1].ReadOnly =
            dgvSession.Columns[0].ReadOnly =
            dgvAdmin.Columns[0].ReadOnly =
            dgvAdmin.Columns[1].ReadOnly = true;



        }


        #region 修改

        private void DgvAdmin_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            var row = dgvAdmin.Rows[e.RowIndex];
            var model = GetUpdateModel<UserInfo>(row);
            userInfoService.Update(model);
        }
        private void DgvSession_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            var row = dgvSession.Rows[e.RowIndex];
            try
            {
                var movieSessionInfo = GetUpdateModel<MovieSessionInfo>(row);
                var session = movieSessionService.GetSessionById(movieSessionInfo.Id);
                session.BroadcastTime = movieSessionInfo.BroadcastTime;
                movieSessionService.Update(session);
            }
            catch (Exception)
            {
                return;
            }


        }
        /// <summary>
        /// MovieType修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DgvType_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            var row = dgvType.Rows[e.RowIndex];
            var model = GetUpdateModel<MovieType>(row);
            movieService.UpdateMovieType(model);
        }
        /// <summary>
        /// MovieInfo修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DgvMovie_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            var row = dgvMovie.Rows[e.RowIndex];
            var modelDto = GetUpdateModel<MovieInfoDto>(row);
            var type = movieService.GetAllMovieType().FirstOrDefault(t => t.TypeName == modelDto.TypeName);
            if (type == null)
            {
                movieService.AddMovieType(new MovieType { TypeName = modelDto.TypeName });
                type = movieService.GetAllMovieType().FirstOrDefault(t => t.TypeName == modelDto.TypeName);
            }
            var model = new MovieInfo()
            {
                Actor = modelDto.Actor,
                Brief = modelDto.Brief,
                Country = modelDto.Country,
                Id = modelDto.Id,
                Direct = modelDto.Direct,
                Image = modelDto.Image,
                Name = modelDto.Name,
                Remark = modelDto.Remark,
                TicketPrice = modelDto.TicketPrice,
                Time = modelDto.Time,
            };
            if (type != null)
                model.Type = type.Id;
            movieService.Update(model);
        }

        #endregion

        private T GetUpdateModel<T>(DataGridViewRow row)
            where T : class, new()
        {
            var type = typeof(T);
            var props = type.GetProperties();

            var entity = Activator.CreateInstance(type);
            for (int i = 0; i < props.Count(); i++)
            {
                props[i].SetValue(entity, row.Cells[i].Value, null);
            }
            return entity as T;
        }

        private void DgvAdmin_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            var type = typeof(UserInfo);
            var index = type.GetProperties().Count();
            if (e.ColumnIndex == index)
            {
                var row = dgvAdmin.Rows[e.RowIndex];
                var id = Convert.ToInt32(row.Cells["Id"].Value);
                if (userInfoService.Delete(id))
                {
                    dgvAdmin.DataSource = null;
                    var admins = userInfoService.GetAdmins();
                    dgvAdmin.DataSource = admins;
                }
            }
        }

        private void DgvSession_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            var type = typeof(MovieSessionInfo);
            var index = type.GetProperties().Count();
            if (e.ColumnIndex == index)
            {
                var row = dgvSession.Rows[e.RowIndex];
                var id = Convert.ToInt32(row.Cells[0].Value);
                if (movieSessionService.Delete(id))
                {
                    dgvSession.DataSource = null;
                    var sessions = movieSessionService.GetAllSession();
                    dgvSession.DataSource = sessions;
                }
            }
        }

        private void DgvType_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            var type = typeof(MovieType);
            var index = type.GetProperties().Count();
            if (e.ColumnIndex == index)
            {
                var row = dgvType.Rows[e.RowIndex];
                var id = Convert.ToInt32(row.Cells[0].Value);
                if (movieService.DeleteMovieType(id))
                {
                    dgvType.DataSource = null;
                    var types = movieService.GetAllMovieType();
                    dgvType.DataSource = types;
                }
            }
        }

        private void DgvMovie_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            var type = typeof(MovieInfoDto);
            var index = type.GetProperties().Count();
            if (e.ColumnIndex == index)
            {
                var row = dgvMovie.Rows[e.RowIndex];
                var id = Convert.ToInt32(row.Cells[0].Value);
                if (movieService.Delete(id))
                {
                    dgvMovie.DataSource = null;
                    var movies = movieService.GetMovieInfos();
                    dgvMovie.DataSource = movies;
                }
            }
        }


        private void 添加电影信息ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FrmMovieInfoAdd frmMovieInfoAdd = new FrmMovieInfoAdd();
            frmMovieInfoAdd.AddSuccess += FrmMovieInfoAdd_AddSuccess;
            frmMovieInfoAdd.ShowDialog();
        }

        private void 查看电影信息列表ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //隐藏其余DataGridView
            dgvSession.Visible = false;
            dgvType.Visible = false;
            dgvAdmin.Visible = false;

            dgvMovie.Visible = true;
            dgvMovie.DataSource = null;
            var movies = movieService.GetMovieInfos();
            dgvMovie.DataSource = movies;
        }

        private void 添加电影类别ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FrmMovieTypeAdd frmMovieTypeAdd = new FrmMovieTypeAdd();
            frmMovieTypeAdd.AddSuccess += FrmMovieTypeAdd_AddSuccess;
            frmMovieTypeAdd.ShowDialog();
        }

        private void 查看电影类别列表ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            dgvMovie.Visible = false;
            dgvSession.Visible = false;
            dgvAdmin.Visible = false;

            dgvType.Visible = true;
            dgvType.DataSource = null;
            var movieTypes = movieService.GetAllMovieType();
            dgvType.DataSource = movieTypes;
        }
        private void 添加电影场次ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FrmMovieSessionAdd frmMovieSessionAdd = new FrmMovieSessionAdd();
            frmMovieSessionAdd.AddSuccess += FrmMovieSessionAdd_AddSuccess;
            frmMovieSessionAdd.ShowDialog();
        }

        #region Add Success Event
        private void FrmMovieInfoAdd_AddSuccess(object sender, EventArgs e)
        {
            if (dgvMovie.Visible == true)
            {
                dgvMovie.DataSource = null;
                var movies = movieService.GetMovieInfos();
                dgvMovie.DataSource = movies;
            }
        }
        private void FrmMovieTypeAdd_AddSuccess(object sender, EventArgs e)
        {
            if (dgvType.Visible == true)
            {
                dgvType.DataSource = null;
                var movies = movieService.GetMovieInfos();
                dgvType.DataSource = movies;
            }
        }
        private void FrmMovieSessionAdd_AddSuccess(object sender, EventArgs e)
        {
            if (dgvSession.Visible == true)
            {
                dgvSession.DataSource = null;
                var sessions = movieSessionService.GetAllSession();
                dgvSession.DataSource = sessions;
            }
        }
        private void FrmRegisterAndAddAdmin_AddSuccess(object sender, EventArgs e)
        {
            if (dgvAdmin.Visible == true)
            {
                dgvAdmin.DataSource = null;
                var admins = userInfoService.GetAdmins();
                dgvAdmin.DataSource = admins;
            }
        }
        #endregion

        private void 查询电影场次ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            dgvMovie.Visible = false;
            dgvType.Visible = false;
            dgvAdmin.Visible = false;

            dgvSession.Visible = true;
            var movieSessions = movieSessionService.GetAllSession();
            dgvSession.DataSource = null;
            dgvSession.DataSource = movieSessions;


        }

        private void 增加管理员ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FrmRegisterAndAddAdmin frmRegisterAndAddAdmin = new FrmRegisterAndAddAdmin(RoleType.Admin);
            frmRegisterAndAddAdmin.AddSuccess += FrmRegisterAndAddAdmin_AddSuccess;
            frmRegisterAndAddAdmin.ShowDialog();
        }

        private void 修改密码ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FrmChangePassword frmChangePassword = new FrmChangePassword();
            frmChangePassword.ShowDialog();
        }

        private void 管理员列表ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            dgvMovie.Visible = false;
            dgvType.Visible = false;
            dgvSession.Visible = false;

            dgvAdmin.Visible = true;
            var admins = userInfoService.GetAdmins();
            dgvAdmin.DataSource = null;
            dgvAdmin.DataSource = admins;

        }

    }
}
